/**
 * @author Bachelor
 */
public class SelectionSort {
    public SelectionSort() {}
    public static <E> void swap(E[] arr, int i, int j) {
        E temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static <E extends Comparable<E>> void sort(E[] arr) {
        for (int i = 0; i < arr.length; i++) {
            // 选择 arr[i...n-1] 中的最小值
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                minIndex = arr[j].compareTo(arr[minIndex]) < 0 ? j : minIndex;
            }
            swap(arr, i, minIndex);
        }
    }

    public static void main(String[] args) {
        Student[] students = {new Student("Alice", 98), new Student("Bobo", 100), new Student("Charles", 66)};
        SelectionSort.sort(students);
        for (Student student : students) {
            System.out.println(student);
        }
    }
}
